package b1w1nn1ng.crossf1t.business;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.servlet.http.HttpServletRequest;

import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;

import b1w1nn1ng.crossf1t.db.Database;
/**
 * the sole purpose of this class is to keep track of all things calendar related.
 * @author Oren
 *
 */
public class CalendarDisplay {
	static int monthCount = 0;
	static Calendar cal = Calendar.getInstance();


	public static String updateCalendar(int month, HttpServletRequest request){
		monthCount += month;
		Date d = new Date();
		cal.setTime(d); //initializes cal to current date
		cal.add(Calendar.MONTH, monthCount); //updates the monthCount of the cal
		cal.set(Calendar.DATE, 1); //puts the cal at the start of the month it's in
		return "calendar.jsp"; //reloads calander page

	}
	
	public static String updateWorkoutCalendar(int month, HttpServletRequest request) {
		monthCount += month;
		Date d = new Date();
		cal.setTime(d); //initializes cal to current date
		cal.add(Calendar.MONTH, monthCount); //updates the monthCount of the cal
		cal.set(Calendar.DATE, 1); //puts the cal at the start of the month it's in
		return "workoutsCalendar.jsp"; //reloads calander page
	}

	public static String resetCalendar(HttpServletRequest request){
		monthCount = 0; //resets the monthCount
		Date d = new Date();
		cal.setTime(d); //initializes cal to current date
		cal.set(Calendar.DATE, 1); //sets cal to start of the month its in (current month)
		return "calendar.jsp"; //reloads the calander page
	}

	public static Calendar getCalendar(HttpServletRequest request){
		return cal;
	}

	public static Calendar currCal(){
		Calendar currCal = Calendar.getInstance();
		currCal.setTime(new Date());
		return currCal;
	}
	public static boolean equals (Calendar cal, Calendar cal2){
		return (cal.get(Calendar.DATE) == cal2.get(Calendar.DATE))
		&& (cal.get(Calendar.MONTH) == cal2.get(Calendar.MONTH))
		&& (cal.get(Calendar.YEAR) == cal2.get(Calendar.YEAR));
	}
	public static String dateToString (Calendar cal){
		String month = "" + cal.get(Calendar.MONTH);
		String date = ""+ cal.get(Calendar.DATE);
		String year = ""+ cal.get(Calendar.YEAR);

		if(date.length() < 2)
			date = "0" + date;
		if(month.length() < 2)
			month = "0" + month;

		String result =  month + "/" + date + "/" + year;
		return result;
	}
	//returns a calendar set to the date of the last workout
	public static Calendar getLastWorkoutDate(User user) throws SQLException, ClassNotFoundException{
		Calendar cal = CalendarDisplay.currCal();
		Workout temp;
		int count = 0;
		if(user.getNumWorkouts() > 0){
			do {
				temp = Database.getWorkout(user.getId(), cal);
				cal.add(Calendar.DATE, -1);
				count ++;
			} while (temp != null && count < 1000);
			return cal;
		}	
		return null;
	}
	
	//@author Chris--Sick of dealing with making Calendars, etc...
	public static Date stringToDate(String setDate) throws ParseException, java.text.ParseException {
		SimpleDateFormat dfm = new SimpleDateFormat("MM/dd/yyyy");
		return dfm.parse(setDate);
	}
	
	public static String dateToString(Date date) {
		SimpleDateFormat dfm = new SimpleDateFormat("MM/dd/yyyy");
		return dfm.format(date);
	}
	
	public static Calendar stringToCalendar(String setDate) throws ParseException, java.text.ParseException {
		SimpleDateFormat dfm = new SimpleDateFormat("MM/dd/yyyy");
		Calendar c = Calendar.getInstance();
		c.setTime(dfm.parse(setDate));
		return c;
	}
	/**
	 * 
	 * @param day - yyyy-mm-dd
	 * @return
	 */
	public static Calendar SQLtoDate(String day){
		Calendar cal = Calendar.getInstance();
		int year = Integer.parseInt(day.substring(0, 4));  
		int month = Integer.parseInt(day.substring(5, 7));
		int date = Integer.parseInt(day.substring(8, 10));
		cal.set(year, month, date);
		return cal;
	}

}



//Today is " + day[cal.get(Calendar.DAY_OF_WEEK)-1] + ", " + month[cal.get(Calendar.MONTH)] + " " + 
//cal.get(Calendar.DATE) + ", " + cal.get(Calendar.YEAR)
